package cn.tedu.erp.project.controller;

import cn.tedu.erp.project.pojo.dto.ProduceMaterialAddNewDTO;
import cn.tedu.erp.project.pojo.dto.ProduceMaterialUpdateDTO;
import cn.tedu.erp.project.pojo.vo.ProduceMaterialListVO;
import cn.tedu.erp.project.pojo.vo.ProduceMaterialStandardVO;
import cn.tedu.erp.project.pojo.vo.ProduceProgressStandardVO;
import cn.tedu.erp.project.service.IProduceMaterialService;
import cn.tedu.erp.project.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@Api(tags = "生产材料管理模块")
@RequestMapping("/produceMaterials")
@RestController
public class ProduceMaterialController {
    @Autowired
    private IProduceMaterialService service;

    // http://localhost:8080/ProduceMaterial/add-new?name=test001&description=hahahaha&sort=100
    // http://localhost:8080/ProduceMaterial/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加生产材料")
    @PreAuthorize("hasAnyAuthority('/erp_maindb/produceMaterial/add-new')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid ProduceMaterialAddNewDTO produceMaterialAddNewDTO) {
        log.debug("开始处理【添加生产材料】的请求业务，参数：{}", produceMaterialAddNewDTO);
        service.addNew(produceMaterialAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8080/ProduceMaterial/delete?id=1
    @PostMapping("/{id}/delete")
    @PreAuthorize("hasAnyAuthority('/erp_maindb/produceMaterial/delete')")
    @ApiOperation("根据id删除生产材料")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "生产材料id", required = true, dataType = "Long"),

    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的id") Long id) {
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改生产材料")
    @PreAuthorize("hasAnyAuthority('/erp_maindb/produceMaterial/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "生产材料id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    })
    public JsonResult<Void> update(@PathVariable @Range(min = 1, message = "请提交有效的id") Long id,
                                   @Valid ProduceMaterialUpdateDTO produceMaterialUpdateDTO) {
        service.updateInfoById(id, produceMaterialUpdateDTO);
        return JsonResult.ok();
    }


    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("查询材料详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<ProduceMaterialStandardVO> getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【查询进度详情】的请求，参数:id{}", id);
        ProduceMaterialStandardVO standardVO = service.getStandardById(id);
        return JsonResult.ok(standardVO);
    }


    @GetMapping("")
    @ApiOperation("查询生产材料")
    @PreAuthorize("hasAnyAuthority('/erp_maindb/produceMaterial/read')")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<ProduceMaterialListVO>> list() {
        log.debug("开始处理【查询列表】的请求，无参数");
        List<ProduceMaterialListVO> list = service.list();
        return JsonResult.ok(list);

    }
}
